<template>
	<view>
		<view style="background-color: #c00000;">
			<uni-search-bar @input="input" v-model="inputValue" :radius="100" cancelButton="none" focus>
			</uni-search-bar>
		</view>
		<!-- 搜索列表 -->
		<view class="sugg-list" v-if="inputValue.length !==0">
			<view class="sugg-item" v-for="(item,index) in searchResultList" :key="item.goods_id"
				@click="gotoGoods_detail(item.goods_id)">
				<text class="goods-name">{{item.goods_name}}</text>
				<uni-icons type="right" size="18"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history_box" v-else>
			<view class="top_box">
				<view class="left_box">
					搜索历史
				</view>
				<view class="right_box">
					<uni-icons type="trash" size="18" @click="clearHistoryList"></uni-icons>
				</view>
			</view>
			<view class="btm_box">
				<uni-tag :text="item" @click="bindClick(item)" class="tag" v-for="(item,index) in historyList"
					:key="index">
				</uni-tag>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				searchResultList: [],
				inputValue: '',
				timer: null,
				historyList: []
			};
		},
		onLoad() {
			// 页面刚一加载就从本地拿去历史纪录
			// const res = uni.getStorageSync('historyList_Key');
			// const res = JSON.parse(uni.getStorageSync('historyList_Key'))
			// console.log(res, typeof res);
			this.historyList = JSON.parse(uni.getStorageSync('historyList_Key') || '[]')
		},
		methods: {
			input(val) {
				// 当搜索框内没值的时候，我们将搜索列表置空并阻止代码
				if (!val) {
					return this.searchResultList = []
				}
				// 通过防抖限制请求的发送频率
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.inputValue = val
					this.getSearchResult()
					this.historyList.unshift(val)
					const set = new Set(this.historyList)
					console.log(set);
					this.historyList = [...set]
					// 将历史纪录保存到本地中
					uni.setStorageSync('historyList_Key', JSON.stringify(this.historyList));
				}, 500)
			},
			async getSearchResult() {
				const {
					data: res
				} = await uni.$http.get('/goods/qsearch', {
					query: this.inputValue
				})
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResultList = res.message
			},
			// 跳转到商品详情也
			gotoGoods_detail(goods_id) {
				uni.navigateTo({
					url: `/pkgGoods/goods_detail/goods_detail?goods_id=${goods_id}`
				})
			},
			// 
			bindClick(item) {
				uni.navigateTo({
					url: '/pkgGoods/goods_list/goods_list?query=' + item
				})
			},
			// 清空历史纪录
			clearHistoryList() {
				this.historyList = [],
					uni.setStorageSync('historyList_Key', '[]')
			}
		}
	}
</script>

<style lang="scss">
	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history_box {
		width: 100%;

		.top_box {
			width: 100%;
			height: 70rpx;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #fff;
			align-items: center;

			.left_box {
				margin-left: 10rpx;
			}

			.right_box {
				margin-right: 10rpx;
			}
		}

		.btm_box {
			margin-top: 10rpx;

			.tag {
				// height: 24rpx;
				// text-align: center;
				// line-height: 24rpx;
				padding: 8rpx;
			}
		}
	}
</style>
